package com.ibeeking.found.cop.b.rest.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.common.enums.SyncStatusEnum;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.cop.b.rest.service.IKfFriendsMessageService;
import com.ibeeking.found.cop.b.rest.service.IMqService;
import com.ibeeking.found.cop.b.rest.service.IWxOpenService;
import com.ibeeking.found.cop.service.common.dos.KfAccountDO;
import com.ibeeking.found.cop.service.common.dos.KfFriendsMessageDO;
import com.ibeeking.found.cop.service.common.enums.MqSyncOperatorTypeEnum;
import com.ibeeking.found.cop.service.common.param.KfFriendsMessageParam;
import com.ibeeking.found.cop.service.common.param.MqSyncCopParam;
import com.ibeeking.found.cop.service.common.query.KfFriendsMessagePageQuery;
import com.ibeeking.found.cop.service.common.vo.KfFriendsMessageVO;
import com.ibeeking.found.cop.service.mapper.KfAccountMapper;
import com.ibeeking.found.cop.service.mapper.KfFriendsMessageMapper;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName KfFriendsMessageServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-01-27 18:50
 **/
@Service
public class KfFriendsMessageServiceImpl extends ServiceImpl<KfFriendsMessageMapper, KfFriendsMessageDO> implements IKfFriendsMessageService {

    @Resource
    private KfFriendsMessageMapper kfFriendsMessageMapper;

    @Resource
    private KfAccountMapper kfAccountMapper;

    @Resource
    private IWxOpenService wxOpenServiceImpl;

    @Resource
    private IMqService mqServiceImpl;

    @Override
    public Page<KfFriendsMessageVO> pageList(KfFriendsMessagePageQuery pageQuery) {
        LambdaQueryWrapper<KfFriendsMessageDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(pageQuery.getId() != null, KfFriendsMessageDO::getId, pageQuery.getId())
                .like(StringUtils.isNotBlank(pageQuery.getFriendsCode()), KfFriendsMessageDO::getFriendsCode, pageQuery.getFriendsCode())
                .eq(pageQuery.getSyncStatus() != null, KfFriendsMessageDO::getSyncStatus, pageQuery.getSyncStatus())
                .orderByDesc(KfFriendsMessageDO::getCreateTime);
        Page<KfFriendsMessageDO> entityDOPages = kfFriendsMessageMapper.selectPage(new Page<KfFriendsMessageDO>(pageQuery.getPageNum(), pageQuery.getPageSize()), wrapper);
        Page<KfFriendsMessageVO> entityVOPages = BeanUtil.convertPage(entityDOPages, KfFriendsMessageVO.class);
        entityVOPages.getRecords().forEach(f ->{
            f.setSyncStatusDesc(SyncStatusEnum.getName(f.getSyncStatus()));
        });
        return entityVOPages;
    }
    /**
     * 从企业微信同步客户信息
     *
     * @param
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer syncKfFriendsMessageFromWx(){
        String tenantId = UserUtils.getTenantId();
        MqSyncCopParam mqParam = new MqSyncCopParam();
        List<KfAccountDO> kfAccountDOS = kfAccountMapper.selectList(new LambdaQueryWrapper<KfAccountDO>().isNotNull(KfAccountDO::getOpenKfid));
        if(Collections3.isNotEmpty(kfAccountDOS)){
            List<String> openKfIdList = kfAccountDOS.stream().map(KfAccountDO::getOpenKfid).collect(Collectors.toList());
            mqParam.setOpenKfIds(openKfIdList);
        }
        mqParam.setTenantId(Long.valueOf(tenantId));
        mqParam.setOperType(MqSyncOperatorTypeEnum.COP_SYNC_KF_FRIENDS_MESSAGE.getCode());
        mqServiceImpl.sendSyncToWxTaskMessage(mqParam);
        return 1;
    }

    @Override
    public Integer saveOrUpdateOrDeleteBatchKfFriendsMessage(Long tenantId, Long accountId, List<KfFriendsMessageParam> params) {
        if(Collections3.isNotEmpty(params)){
           List<KfFriendsMessageDO> allKfFriendsMessageDOS = new ArrayList<>();
            for(KfFriendsMessageParam param:params){
                KfFriendsMessageDO friendsMessageDO = BeanUtil.convertBean(param,KfFriendsMessageDO.class);

                allKfFriendsMessageDOS.add(friendsMessageDO);
            }
            if(Collections3.isNotEmpty(allKfFriendsMessageDOS)){
                kfFriendsMessageMapper.insertBatchSomeColumn(allKfFriendsMessageDOS);
            }
            return 1;
        }
        return 0;
    }
}
